﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using HeadLinx.AD.Explorer.Properties;
using HeadLinx.AD.Common;

namespace HeadLinx.AD.Explorer
{
    /// <summary>
    /// Interaction logic for ShowLinksDialog.xaml
    /// </summary>
    public partial class ShowLinksDialog : Window
    {
        public ShowLinksDialog()
        {
            InitializeComponent();
            chkIncludeMe.Content = String.Format((String)chkIncludeMe.Content, LocalUtility.GetCurrentUserAlias());
            chkRememberList.IsChecked = Settings.Default.RememberList;
            chkIncludeMe.IsChecked = Settings.Default.IncludeMe;
            txtNames.Text = Settings.Default.NameList;
        }

        public string SelectNames
        {
            get
            {
                if (chkIncludeMe.IsChecked == true)
                {
                    return LocalUtility.GetCurrentUserAlias() + ";" + txtNames.Text;
                }

                return txtNames.Text;
            }
        }

        private void txtNames_TextChanged(object sender, TextChangedEventArgs e)
        {
            if (txtNames.Text.Length > 0)
            {
                btnDrawChart.IsEnabled = true;
            }
            else
            {
                btnDrawChart.IsEnabled = true;
            }

            SaveSettings();
        }

        private void btnDrawChart_Click(object sender, RoutedEventArgs e)
        {
            LocalUtility.RunQueryAndShowResults(this, HeadLinxQueryType.Arbitrary, this.SelectNames, null, false);
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            txtNames.Focus();
        }

        private void SaveSettings()
        {
            if (chkRememberList.IsChecked == true)
            {
                Settings.Default.NameList = txtNames.Text;
            }
            else
            {
                Settings.Default.NameList = String.Empty;
            }

            Settings.Default.RememberList = (chkRememberList.IsChecked == true);
            Settings.Default.IncludeMe = (chkIncludeMe.IsChecked == true);

            Settings.Default.Save();
        }

        private void chkRememberList_Click(object sender, RoutedEventArgs e)
        {
            SaveSettings();
        }

        private void chkIncludeMe_Click(object sender, RoutedEventArgs e)
        {
            SaveSettings();
        }
    }
}
